package com.hollysys.hollimsa.biz.istio.gateway.service.impl;


import com.hollysys.hollimsa.biz.istio.gateway.dao.GatewayDao;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayDTO;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayDictDTO;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayVO;
import com.hollysys.hollimsa.biz.istio.gateway.service.GatewayService;
import com.hollysys.hollimsa.common.filter.tenantinfo.TenantInfoContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class GatewayServiceImpl implements GatewayService {

    @Autowired
    private GatewayDao gatewayDao;

    /**
     * 增加网关
     *
     * @param gateway 网关信息
     * @return 返回值状态
     */
    @Override
    public boolean addGateway(GatewayVO gateway) {
        final String namespace = TenantInfoContext.getTenantNS();
        gatewayDao.createistioGateWay(gateway, namespace);
        return Boolean.TRUE;
    }


    /**
     * 修改网关信息
     *
     * @param gateway 网关信息
     * @return 返回值状态
     */
    @Override
    public boolean editGateway(GatewayVO gateway) {
        final String namespace = TenantInfoContext.getTenantNS();
        gatewayDao.editistioGateWay(gateway, namespace);
        return Boolean.TRUE;
    }


    /**
     * 删除网关
     *
     * @param name 网关名称
     * @return 返回状态
     */
    @Override
    public boolean deleteGateway(String name) {
        final String namespace = TenantInfoContext.getTenantNS();
        return gatewayDao.deleteGateway(name, namespace);
    }


    @Override
    public List<GatewayDTO> listGateway() {
        final String namespace = TenantInfoContext.getTenantNS();
        return gatewayDao.listGateway(namespace);
    }


    /**
     * 获取网关下拉数据
     *
     * @return
     */
    @Override
    public List<GatewayDictDTO> gatewayDropDownData() {
        final String namespace = TenantInfoContext.getTenantNS();
        return gatewayDao.gatewayDropDownData(namespace).stream().map(gateway -> {
            List<GatewayDictDTO.Children> servers = gateway.getSpec().getServers().stream().map(server -> {

                    /*给出前端树状推导*/
                    List<GatewayDictDTO.Children> hosts = server.getHosts().stream().map(host -> GatewayDictDTO.Children.builder()
                        .id(gateway.getMetadata().getName() + "#" + server.getPort().getName() + "#" + host)
                        .label(host)
                        .value(host).build()).collect(Collectors.toList());

                    return GatewayDictDTO.Children.builder()
                        .id(gateway.getMetadata().getName() + "#" + server.getPort().getName())
                        .label(server.getPort().getName())
                        .value(server.getPort().getName())
                        .children(hosts).build();
                }
            ).collect(Collectors.toList());

            return GatewayDictDTO.builder()
                .id(gateway.getMetadata().getName())
                .label(gateway.getMetadata().getName())
                .value(gateway.getMetadata().getName())
                .children(servers).build();
        }).collect(Collectors.toList());
    }
}
